/*
 * A binary search tree implementation.
 * Copyright 2009 sevenever <sevenever@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _SEVENEVER_BSTREE_H_
#define _SEVENEVER_BSTREE_H_

typedef enum
{
  preorder,
  postorder,
  endorder,
  leaf
}
visit;

typedef struct __bst* pbst;

//value compare func, should return 0 if equal and <0 if first is smaller than second.
typedef int (*bst_compare_func)(void*,void*);
//walk func
typedef void (*bst_act_func)(void*, visit, int);
//free func
typedef void (*bst_free_func)(void*);

//construct a new bst
pbst bst_new(bst_compare_func);
//delete a bst, 
//if bst_act_func parameter is not null, call it before delete a node
void bst_dispose(pbst, bst_free_func);

//insert a data into tree, return inserted node pointer.
pbst bst_insert(pbst, void *);
//delete a data from tree
void bst_delete(pbst, void *);
//search in a tree, return 0 if not found
void * bst_search(pbst, void *);
//walk through the tree
void bst_walk(pbst, bst_act_func);

#endif //_SEVENEVER_BSTREE_H_
